﻿using HslCommunication.Profinet.Siemens;
using NLog;
using NLog.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ZhonTai.Admin.Core;
using ZhonTai.Admin.Tools.Cache;
using ZhonTai.Industry.Contracts.Core.Enums;
using ZhonTai.Industry.Core.Consts;
using ZhonTai.Industry.Domain.StationFeature;
using ZhonTai.Industry.Domain.WorkStation;
using ZhonTai.Industry.Repositories.StationFeature;
using ZhonTai.Industry.Repositories.WorkStation;
using ZhonTai.Industry.Services.MesInit;

namespace ZhonTai.Industry.StartUp
{
    public class MesApp
    {
        #region 服务获取

        private readonly ILogger logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
        private readonly ICacheTool cacheTool = AppInfo.GetRequiredService<ICacheTool>();
        private readonly MesInit mesInit = AppInfo.GetRequiredService<MesInit>();
        private readonly WorkStationRepository workStationRepository = AppInfo.GetRequiredService<WorkStationRepository>();
        private readonly StationFeautureRepository stationFeautureRepository = AppInfo.GetRequiredService<StationFeautureRepository>();

        #endregion 服务获取

        /// <summary>
        /// MES运行
        /// </summary>
        /// <param name="args"></param>
        /// <param name="assembly"></param>
        public async Task RunAsync(string[] args, Assembly assembly = null)
        {
            logger.Info("MES APP Startup");
            HslCommunication.Authorization.SetAuthorizationCode("b980977c-3323-4876-b633-c0bef93d75c1");
            //return;
            try
            {
                await mesInit.InitProductDataAsync();
                List<WorkStationEntity> workStationList = await workStationRepository.Select
                    .Where(w => w.Enabled)
                    .Include(x => x.ProductionLine)
                    .ToListAsync();
                foreach (var x in workStationList)
                {
                    await cacheTool.SetAsync(CacheKey.StationId + x.StationCode, x.Id);
                    List<StationFeatureEntity> stationFeatures = stationFeautureRepository.Select
                    .Where(w => w.StationId == x.Id && w.Enabled)
                    .Include(y => y.WorkStation)
                    .Include(z => z.Feature)
                    .ToList();
                    bool runPLC = false;
                    if (stationFeatures.Any(a => a.Feature.FeatureCode == nameof(FeatureTypeEnum.PLC)))
                    {
                        runPLC = await mesInit.InitPlcAsync(x);
                    }
                    foreach (var v in stationFeatures)
                    {
                        switch (v.Feature.FeatureCode)
                        {
                            case nameof(FeatureTypeEnum.PLC): if (runPLC) await mesInit.InitProcessParmeterAsync(x); break;

                            case nameof(FeatureTypeEnum.Route): await mesInit.InitProductRouteAsync(x, v); break;
                            case nameof(FeatureTypeEnum.AlarmErr): await mesInit.InitAlarmErrAsync(x); break;
                            default:
                                break;
                        }
                    }
                }
                logger.Info("MES APP Running");
            }
            catch (Exception ex)
            {
                logger.Info("MES APP ShutDown;message:" + ex.Message);
            }
        }
    }
}